﻿using Dracham.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Dracham.Components.GameScreens
{
    public class SplashScreen : IGameScreen
    {
        #region Fields

        private bool skipable;
        private int duration;
        private double elapsed;
        private Vector2 location;
        private Texture2D texture;
        private Keys[] skipKeys;
        private SpriteBlendMode blendMode;

        #endregion

        #region Constructors

        public SplashScreen(int duration, Texture2D texture) : this(duration, texture, true, null) { }
        public SplashScreen(int duration, Texture2D texture, bool skipable) : this(duration, texture, skipable, null) { }
        public SplashScreen(int duration, Texture2D texture, bool skipable, Keys[] skipKeys) : this(duration, texture, new Vector2(((Dracham.Instance.GraphicsDevice.Viewport.Width - texture.Width) / 2), ((Dracham.Instance.GraphicsDevice.Viewport.Height - texture.Height) / 2)), skipable, skipKeys) { }
        public SplashScreen(int duration, Texture2D texture, Vector2 location) : this(duration, texture, location, true, null) { }
        public SplashScreen(int duration, Texture2D texture, Vector2 location, bool skipable) : this(duration, texture, location, true, null) { }
        public SplashScreen(int duration, Texture2D texture, Vector2 location, bool skipable, Keys[] skipKeys)
        {
            this.skipable = skipable;
            this.duration = duration;
            this.location = location;
            this.texture = texture;
            this.skipKeys = skipKeys;

            this.blendMode = SpriteBlendMode.AlphaBlend;
        }

        #endregion

        #region Properties

        public bool Skipable
        {
            get { return skipable; }
            set { skipable = value; }
        }

        public int Duration
        {
            get { return duration; }
            set { duration = value; }
        }

        public double Elapsed
        {
            get { return elapsed; }
        }

        public Vector2 Location
        {
            get { return location; }
            set { location = value; }
        }

        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        public Keys[] SkipKeys
        {
            get { return skipKeys; }
            set { skipKeys = value; }
        }

        public SpriteBlendMode BlendMode
        {
            get { return blendMode; }
            set { blendMode = value; }
        }

        #endregion

        #region Methods

        #region IGameScreen Members

        public void Draw(GameTime gameTime)
        {
            Dracham.Instance.SpriteBatch.Begin(this.blendMode);

            Dracham.Instance.SpriteBatch.Draw(this.texture, this.location, Color.White);

            Dracham.Instance.SpriteBatch.End();
        }

        public bool Update(GameTime gameTime)
        {
            this.elapsed += gameTime.ElapsedGameTime.TotalMilliseconds;

            if (this.elapsed > this.duration) return true;

            if (!this.skipable) return false;

            if (this.skipKeys == null)
            {
                // if any key is down
                // return true
            }
            else
            {
                foreach (Keys key in this.skipKeys)
                {
                    // Check if key was pressed
                    // return true if key was pressed
                }
            }

            return false;
        }

        #endregion

        #endregion
    }
}